package info.thereisonlywe.onenesspathfinder.configuration;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;

public class ConfigurationDialog {

	private static JDialog dialog;
	private static JLabel label_Language;
	private static JComboBox<String> comboBox_Language;

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setIconImage(null);
		dialog.setTitle("Oneness Pathfinder "
				+ ProjectLocalization.getString(ProjectConstants.CORE_PROJECT,
						"Configuration"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setResizable(false);
		dialog.setPreferredSize(new Dimension(340, 220));
		dialog.setSize(new Dimension(260, 75));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		dialog.getContentPane().add(panel, BorderLayout.CENTER);
		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				exit();
			}
		});

		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.TRAILING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(getLanguageLabel(),
								GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getLanguageComboBox(), 0, 145,
								Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														getLanguageLabel(),
														GroupLayout.PREFERRED_SIZE,
														29,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														getLanguageComboBox(),
														GroupLayout.PREFERRED_SIZE,
														26,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		panel.setLayout(gl_panel);

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
							{
								exit();
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

	}

	private static void exit()
	{
		Main.startupSettings.set("language",
				(String) comboBox_Language.getSelectedItem());
		Main.startupSettings.write();
		dialog.setVisible(false);
		dialog.dispose();
		System.exit(0);
	}

	public synchronized static void showDialog()
	{
		init();
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		dialog.setVisible(true);
	}

	protected static JLabel getLanguageLabel()
	{
		if (label_Language == null)
		{
			label_Language = new JLabel(ProjectLocalization.getString(
					ProjectConstants.CORE_PROJECT, "Language"));
		}
		return label_Language;
	}

	protected static JComboBox<String> getLanguageComboBox()
	{
		if (comboBox_Language == null)
		{
			comboBox_Language = new JComboBox<>();
			comboBox_Language.setModel(new DefaultComboBoxModel<String>(
					new String[] { "English", "Türkçe" }));
			comboBox_Language.getInputMap().put(
					KeyStroke.getKeyStroke("SPACE"), "none");
			comboBox_Language.setSelectedItem(Main.startupSettings
					.getString("language"));
		}
		return comboBox_Language;
	}

}
